﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TCHSHue.DAL;

namespace TCHSHue.BLL
{
    public enum Quyen
    {
        Admin, VietBai
    }
    public class UserExtention : User
    {
        public string strQuyen { get { if (quyen == ((int)Quyen.Admin).ToString()) return "Quản trị viên"; else return "Biên tập viên"; } set { } }
    }
    public class UserBLL
    {
        public static List<UserExtention> getList()
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = from e in data.Users
                         orderby e.tenDangNhap ascending
                         select new UserExtention { tenDangNhap = e.tenDangNhap, email = e.email, quyen = e.quyen, hoTen = e.hoTen };
                return ls.ToList();
            }
        }
        public static bool create(string tenDangNhap, string matKhau, string hoTen, string email, string quyen)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = (from e in data.Users
                          where e.tenDangNhap == tenDangNhap
                          select e).SingleOrDefault();
                if (ls != null)
                    return false;
                User user = new User { tenDangNhap = tenDangNhap, matKhau = matKhau, hoTen = hoTen, email = email, quyen = quyen };
                data.Users.InsertOnSubmit(user);
                data.SubmitChanges();
                return true;
            }
        }

        internal static bool delete(string tenDangNhap)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var user = (from e in data.Users
                            where e.tenDangNhap == tenDangNhap
                            select e).SingleOrDefault();
                if (user != null)
                {
                    data.Users.DeleteOnSubmit(user);
                    data.SubmitChanges();
                    return true;
                }
                return false;
            }

        }

        internal static User getUser(string tenDangNhap, string matKhau)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var user = (from e in data.Users
                            where e.tenDangNhap == tenDangNhap && e.matKhau == matKhau
                            select e).SingleOrDefault();
                return user;
            }
        }

        internal static object update(User user)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var us = (from e in data.Users
                          where e.tenDangNhap == user.tenDangNhap
                          select e).SingleOrDefault();
                us.matKhau = user.matKhau;
                us.hoTen = user.hoTen;
                us.soDienThoai = user.soDienThoai;
                us.quyen = user.quyen;
                data.SubmitChanges();
                return us;
            }
        }

        internal static User searchEmail(string email)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var us = (from e in data.Users
                          where e.email == email
                          select e).SingleOrDefault();
                return us;
            }
        }

        internal static bool update(string tenDangNhap, string hoTen, string email)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var user = (from e in data.Users
                            where e.tenDangNhap == tenDangNhap
                            select e).SingleOrDefault();
                if (user != null)
                {
                    user.hoTen = hoTen;
                    user.email = email;
                }
                data.SubmitChanges();
                return true;
            }
        }
    }
}